package capstone.tagit;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.ToggleButton;

/**
 * 
 * The activity for setting user options.
 * Includes options for setting which tag types will be displayed on screen,
 * Which tag color is currently selected,
 * 		Note: this option is included in the tagging activity.
 * 		It should be removed from here or a configurable option as to whether
 * 		it should be here, there, or both.
 * Includes GPS on/off switch.
 * 
 *
 */
public class MenuActivity extends Activity {
	/**
	 * A SharedPreferences object that shares information between the Menu and the 
	 * main map activity.  Used to transfer information about GPS status, TagType filtering,
	 * and current tagging color.
	 * Note: Current tagging color might not should be included in MenuActivity.  This should
	 * probably be addressed in a configurability option.
	 */
	SharedPreferences menuSettings;
	/**
	 * A Shared Preferences Editor that allows changes to be made to menuSettings.
	 */
	SharedPreferences.Editor prefEditor;
	
	/**
	 * The list of tagTypes available for the app.
	 */
	ArrayList<String> tagTypes;
	/**
	 * A list of boolean states corresponding to the checkboxes
	 * for each TagType.  Must be kept mathcing with the information in menuSettings.
	 */
	ArrayList<Boolean> tagStates;
	
	/**
	 * The method called upon creation of this activity.  Sets up menuSettings, sets the ContentView
	 * as the settings.xml file in res/layout.  Sets the tagTypes as given by the calling Activity.
	 * Sets the tagStates list.  Configures the CheckBoxes corresponding to each type and the Spinner
	 * for current tag color selection.  Sets up other UI settings.
	 * 
	 * @param savedInstanceState an available Bundle that can save state to facilitate re-instantiation of this Activity.
	 * Not currently used.
	 */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		menuSettings = getSharedPreferences("MenuPref", MODE_PRIVATE);
		prefEditor = menuSettings.edit();
		setContentView(R.layout.settings);
		Intent myIntent = getIntent();
		tagTypes = myIntent.getStringArrayListExtra("tagtypes");
		tagStates = new ArrayList<Boolean>();
		setStates();
		configureCheckBoxes();
		configureSpinner();
		
		ToggleButton GPStoggle = (ToggleButton) findViewById(R.id.GPStoggle);
		GPStoggle.setChecked(menuSettings.getBoolean("GPS_STATUS",true));
		GPStoggle.setOnClickListener(GPSlistener);
		
		Button backButton = (Button) findViewById(R.id.backButton);
		backButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				setResult(RESULT_OK, new Intent());
				finish();
			}
		});
	}
	/**
	 * The GPS toggle button Listener.
	 * Sets option in menuSettings corresponding to GPS status.
	 */
	OnClickListener GPSlistener = new OnClickListener(){
		public void onClick(View view){
			ToggleButton button = (ToggleButton) view;
			//button.setChecked(!button.isChecked());
			prefEditor.putBoolean("GPS_STATUS",button.isChecked());
			prefEditor.commit();
		}
		
	};
	
	/**
	 * Sets all the states in the tagStates list.
	 * Initializes them to a default of true.
	 */
	void setStates(){
		for (String tagType : tagTypes){
			tagStates.add(menuSettings.getBoolean(tagType,true));
		}
	}
	
	/**
	 * Creates all the CheckBoxes which will be placed onscreen in the 
	 * configurable space.  Creates a CheckBox for each TagType
	 */
	void configureCheckBoxes(){
		ViewGroup checkBoxLayout = (ViewGroup)findViewById(R.id.checkBoxLayout);
		int currentTypeIndex = 0;
		for (String tagTypeName : tagTypes){
			MyCheckBox checkBox = new MyCheckBox(this, currentTypeIndex);
			checkBox.setText(tagTypeName);
			checkBoxLayout.addView(checkBox);
			currentTypeIndex++;
		}
	}
	
	/**
	 * Configures the color selection Spinner.
	 * Note: Current tagging color might not should be included in MenuActivity.  This should
	 * probably be addressed in a configurability option.  Creates a Spinner option for each TagType
	 */
	void configureSpinner(){
		Spinner spinner = (Spinner) findViewById(R.id.selectSpinner);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,
				tagTypes);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(menuSettings.getInt("COLOR_CHOICE", 0));
		spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
				prefEditor.putInt("COLOR_CHOICE",pos);
				prefEditor.commit();
			}
			public void onNothingSelected(AdapterView<?> parent){
				// do nothing
			}
		});
	}

	/**
	 * The CheckBoxes used for this menu.  Automatically sets the listener to configure the menuSettings
	 * based on its type.
	 */
	class MyCheckBox extends CheckBox{
		/**
		 * This object's index within the tagTypes and tagStates arrays.
		 */
		int index;
		
		MyCheckBox(Context c, int i){
			super(c);
			index = i;
			setChecked(tagStates.get(index));
			setOnClickListener(new View.OnClickListener(){
				public void onClick(View view){
					boolean state = !tagStates.get(index);
					tagStates.set(index, state);
					prefEditor.putBoolean(tagTypes.get(index),state);
					prefEditor.commit();
				}
			});
		}
		
		public void setIndex(int index){
			this.index=index;
		}
		public int getIndex(){
			return index;
		}
	}
}